package com.example.qu_tao.presenter;

import com.example.qu_tao.contract.ShouyeContract;
import com.example.qu_tao.database.GoodsDao;
import com.example.qu_tao.entity.Goods;

import java.util.ArrayList;
import java.util.List;

public class ShouyePersenter implements ShouyeContract.IShouyeContract {
    private ShouyeContract.IShouyeView mView;
    private GoodsDao goodsDao;

    public ShouyePersenter(ShouyeContract.IShouyeView view, android.content.Context context) {
        mView = view;
        goodsDao = new GoodsDao(context);
    }

    @Override
    public void getGoodsList() {
        List<Goods> goodsList = goodsDao.getAllGoods();
        mView.showGoodsLisst(goodsList);
    }

    @Override
    public void searchGoods(String keyword) {
        List<Goods> allGoods = goodsDao.getAllGoods();
        List<Goods> searchResult = new ArrayList<>();
        for (Goods goods : allGoods) {
            if (goods.getGoods_name().contains(keyword)) {
                searchResult.add(goods);
            }
        }
        mView.showGoodsLisst(searchResult);
    }
}